package org.fengling.readingplatform.manager.controller;

import io.swagger.v3.oas.annotations.tags.Tag;
import org.fengling.readingplatform.manager.service.ChapterService;
import org.fengling.readingplatform.model.entity.novel.Chapter;
import org.fengling.readingplatform.model.entity.novel.ChapterUpdateInfo;
import org.fengling.readingplatform.model.vo.common.Result;
import org.fengling.readingplatform.model.vo.common.ResultCodeEnum;
import org.fengling.readingplatform.model.vo.novel.ChapterVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "章节管理接口")
@RestController
@RequestMapping(value="/admin/novel/chapter")
public class ChapterController {

    @Autowired
    private ChapterService chapterService;

    //章节查询
    @GetMapping("/getChapter/{novelId}/{chapterId}")
    public Result getChapter(@PathVariable("novelId") Long novelId,
                             @PathVariable("chapterId") Long chapterId){
        Chapter chapter = chapterService.getChapter(novelId, chapterId);
        return Result.build(chapter, ResultCodeEnum.SUCCESS);
    }

    // 查询章节列表
    @GetMapping("/getChapterList/{novelId}/{isDeleted}")
    public Result getChapterList(@PathVariable("novelId") Long novelId,
                                 @PathVariable("isDeleted") Integer isDeleted){
        List<ChapterVo> list = chapterService.getChapterList(novelId, isDeleted);
        return Result.build(list , ResultCodeEnum.SUCCESS) ;
    }

    // 章节添加的方法
    @PostMapping("/saveChapter")
    public Result saveChapter(@RequestBody Chapter chapter){
        System.out.println(chapter);
        System.out.println("ggggggggggggggggggggggggggggggggggggggggg");
        chapterService.saveChapter(chapter);
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }

    // 章节放入草稿箱的的方法
    @DeleteMapping(value = "/deleteChapter/{novelId}/{chapterId}")
    public Result deleteChapter(@PathVariable("novelId") Long novelId,
                                @PathVariable("chapterId") Long chapterId) {
        chapterService.deleteChapter(novelId, chapterId);
        return Result.build(null , ResultCodeEnum.SUCCESS) ;
    }

    // 章节彻底删除的方法
    @DeleteMapping("/delete/{novelId}/{chapterId}")
    public Result delete(@PathVariable("novelId") Long novelId,
                         @PathVariable("chapterId") Long chapterId){
        chapterService.delete(novelId, chapterId);
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }

    // 章节状态改变的方式
    @PutMapping("/changeStatus/{novelId}/{chapterId}/{isDeleted}")
    public Result changeStatus(@PathVariable("novelId") Long novelId,
                               @PathVariable("chapterId") Long chapterId,
                               @PathVariable("isDeleted") Integer isDeleted){
        chapterService.changeStatus(novelId, chapterId, isDeleted);
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }

    // 获取小说章节最新信息
    @GetMapping("/getChapterUpdateInfo/{novelId}")
    public Result getChapterUpdateInfo(@PathVariable("novelId") Long novelId){
        ChapterUpdateInfo chapterUpdateInfo = chapterService.getChapterUpdateInfo(novelId);
        return Result.build(chapterUpdateInfo, ResultCodeEnum.SUCCESS);
    }

    // 修改章节的方法(有可能是插入)
    @PutMapping("/updateChapter")
    public Result updateChapter(@RequestBody Chapter chapter){
        System.out.println(chapter);
        chapterService.updateChapter(chapter);
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }
}
